//
//  FQHelpCenterViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/25.
//

import UIKit
import JXSegmentedView

class FQHelpCenterViewController: BaseViewController, JXSegmentedViewDelegate, UITableViewDataSource, UITableViewDelegate {
    let segmentedDataSource = JXSegmentedTitleDataSource()
    let segmentedView = JXSegmentedView()

    let mainTable = UITableView(frame: .zero, style: .plain)
    
    var dataArray = [FQHelpCenterModel]()
    let imageV = UIImageView(imageString: "icon_helpCenterBackground")

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .clear
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        safeAreaBottomView.backgroundColor = .white
        
        let path : URL = Bundle.main.url(forResource: "HelpCenter", withExtension: "plist")!
        let data : Data = try! Data(contentsOf: path)
        let dataSource = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [[String : Any]]
        dataArray = dataSource.map({ dict in
            FQHelpCenterModel.deserialize(from: dict)!
        })
        
        imageV.isUserInteractionEnabled = true
        view.addSubview(imageV)
        
        imageV.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(255~)
        }
        
        let titleLb = QMUILabel(font: .mSFont(18), textColor: .color51)
        titleLb.text = "Hi～，有什么可以帮您！"
        imageV.addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(28)
            make.bottom.equalTo(-115~)
        }
        
        let subTitleLb = QMUILabel(font: .rSFont(14), textColor: .color51)
        subTitleLb.text = "宝银钱包为您服务"
        imageV.addSubview(subTitleLb)
        
        subTitleLb.snp.makeConstraints { make in
            make.left.equalTo(titleLb)
            make.bottom.equalTo(-89~)
        }
        
        let whiteV = UIView()
        whiteV.backgroundColor = .white
        view.addSubview(whiteV)
        
        whiteV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.height.equalTo(50)
        }
        
        let button1 = QMUIButton(title: "问题反馈", titleColor: .color51, font: .rSFont(16))
        button1.imagePosition = .left
        button1.spacingBetweenImageAndTitle = 5
        button1.setImage(UIImage(named: "icon_helpCenter1"), for: .normal)
        button1.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        button1.tag = 1
        whiteV.addSubview(button1)
        
        button1.snp.makeConstraints { make in
            make.left.top.bottom.equalToSuperview()
            make.right.equalTo(whiteV.snp.centerX)
        }
        
        let button2 = QMUIButton(title: "机构热线", titleColor: .color51, font: .rSFont(16))
        button2.imagePosition = .left
        button2.spacingBetweenImageAndTitle = 5
        button2.setImage(UIImage(named: "icon_helpCenter2"), for: .normal)
        button2.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        button2.tag = 2
        whiteV.addSubview(button2)
        
        button2.snp.makeConstraints { make in
            make.right.top.bottom.equalToSuperview()
            make.left.equalTo(whiteV.snp.centerX)
        }
        
        segmentedDataSource.itemSpacing = 20~
        segmentedDataSource.titleNormalColor = .color102
        segmentedDataSource.titleSelectedColor = .color51
        segmentedDataSource.titleNormalFont = .rSFont(15)
        segmentedDataSource.titleSelectedFont = .mSFont(18)
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.itemWidth = JXSegmentedViewAutomaticDimension
        segmentedDataSource.titles = dataArray.map({ $0.title })
        
        segmentedView.dataSource = segmentedDataSource
        segmentedView.delegate = self
        segmentedView.backgroundColor = .white
        segmentedView.clipsToBounds = true
        segmentedView.layer.cornerRadius = 8
        segmentedView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        imageV.addSubview(segmentedView)
        
        segmentedView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(50)
            make.bottom.equalToSuperview()
        }
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorHeight = 4
        indicator.indicatorColor = .colorMain
        indicator.verticalOffset = 2
        indicator.indicatorCornerRadius = 2
        segmentedView.indicators = [indicator]
                
        mainTable.rowHeight = 50
        mainTable.dataSource = self
        mainTable.delegate = self
        mainTable.backgroundColor = .white
        mainTable.layer.cornerRadius = 8
        mainTable.clipsToBounds = true
        mainTable.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
        mainTable.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        view.addSubview(mainTable)
        
        mainTable.snp.makeConstraints { make in
            make.left.right.equalTo(segmentedView)
            make.top.equalTo(imageV.snp.bottom)
            make.height.equalTo(50 * 3)
        }
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        mainTable.snp.remakeConstraints { make in
            make.left.right.equalTo(segmentedView)
            make.top.equalTo(imageV.snp.bottom)
            make.height.equalTo(50 * dataArray[index].QAndA.count)
        }
        mainTable.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataArray[segmentedView.selectedIndex].QAndA.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell?.accessoryType = .disclosureIndicator
        cell?.textLabel?.text = "\(indexPath.row + 1)." + dataArray[segmentedView.selectedIndex].QAndA[indexPath.row].question
        cell?.textLabel?.font = .rSFont(15)
        cell?.textLabel?.textColor = .color51
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let vc = FQHelpCenterDetailViewController(indexPath: indexPath, baseModel: dataArray[segmentedView.selectedIndex])
        navigationController?.qmui_pushViewController(vc, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        .leastNonzeroMagnitude
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        nil
    }
    
    @objc func buttonClick(_ button : QMUIButton) {
        if button.tag == 1 {
            navigationController?.qmui_pushViewController(FQFeedbackViewController(), animated: true)
        } else {
            navigationController?.qmui_pushViewController(FQOrganizationPhoneViewController(), animated: true)
        }
    }
    
    override func setupNavigationItems() {
        super.setupNavigationItems()
        title = "帮助中心"
    }
    
    override func qmui_navigationBarTintColor() -> UIColor? {
        .black
    }
    
    override func qmui_titleViewTintColor() -> UIColor? {
        .black
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        .default
    }
    
    override func customNavigationBarTransitionKey() -> String? {
        "help"
    }
}
